<template>
  <div>
    <!-- 面包屑导航 -->
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>商品管理</el-breadcrumb-item>
        <el-breadcrumb-item>商品列表</el-breadcrumb-item>
      </el-breadcrumb>
    <!-- / 面包屑导航 -->

    <!-- 卡片视图 -->
    <el-card>
      <el-row :gutter='20'>
        <el-col :span='8'>
          <el-input clearable @clear="getGoodsList" v-model="queryData.query" placeholder="请输入内容" class='input-with-select'>
            <el-button @click='getGoodsList' slot='append' icon='el-icon-search'></el-button>
          </el-input>
        </el-col>
        <el-col :span='4'>
          <el-button @click='$router.push("/goods/add")' type="primary">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 商品列表区 -->
      <el-table border stripe :data="goods">
        <el-table-column type='index' label="#"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column width="100" prop="goods_price" label="商品价格(元)"></el-table-column>
        <el-table-column width="100" prop="goods_weight" label="商品重量"></el-table-column>
        <el-table-column width="150" label="创建时间">
          <template slot-scope="scope">
            {{scope.row.upd_time | formatDate}}
          </template>
        </el-table-column>
        <el-table-column width="150" label="操作">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini"></el-button>
            <el-button @click="removeGoods(scope.row.goods_id)" type="danger" icon="el-icon-delete" size="mini"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      background
      :current-page="queryData.pagenum"
      :page-sizes="[5, 10, 20, 30]"
      :page-size="queryData.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    </el-card>
    <!-- / 卡片视图 -->
  </div>
</template>

<script>
export default {
  data () {
    return {
      queryData: {
        query: '',
        pagenum: 1,
        pagesize: 5
      },
      goods: [],
      total: 0
    }
  },

  created () {
    this.getGoodsList()
  },

  methods: {
    // 获取商品列表
    async getGoodsList () {
      const { data: res } = await this.$axios.get('goods', {
        params: this.queryData
      })
      if (res.meta.status !== 200) return this.$message.error('获取失败!')
      this.goods = res.data.goods
      this.total = res.data.total
    },
    // 选择页码条数
    handleSizeChange (val) {
      this.queryData.pagesize = val
      this.getGoodsList()
    },
    // 页码发生变化
    handleCurrentChange (val) {
      this.queryData.pagenum = val
      this.getGoodsList()
    },
    // 删除商品
    removeGoods (id) {
      this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async _ => {
        const { data: res } = await this.$axios.delete('goods/' + id)
        if (res.meta.status !== 200) return this.$message.error('删除失败!')
        this.$message.success('删除成功!')
        this.getGoodsList()
      }).catch(() => {
        this.$message('已取消删除')
      })
    }
  }
}
</script>

<style scoped lang='less'>

</style>
